package com.llc.station.kcp.ui.core;

import com.llc.station.kcp.ui.core.bean.MacCar;
import com.llc.station.kcp.ui.core.component.*;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;

import java.util.Arrays;

/**
 * 作者 zxj<br>
 * 时间 2017/9/12 9:24
 * 说明 ...
 */
public class ComponentHelper {
    public final static String OIL_TYPE_92 = "92";
    public final static String OIL_TYPE_95 = "95";
    public final static String OIL_TYPE_98 = "98";
    private int width;
    private int height;

    private int piceWidth = 40;

    private double titleHeightPer = 0.3;//标题高度占整体的百分比
    private double titleHeight;//标题高度

    private double contentHeightPer = 0.4;//主要内容区域所占百分比
    private double contentHeight;//内容高度区域

    private int gridPadding = 10;//中心格子的间距
    private double gridWidth; //中心区，每个格子的宽度
    private double waitGridHeightPer = 0.45;
    private double waitGridHeight;//中心区，上方等待车辆列表的高度
    private double mainContentGridHeight;//中心区，主要显示区的高度

    private double bottomTitlePer = 0.2;
    private double bottomFlowPer = 0.5;
    private double bottomLockPer = 0.3;
    private double bottomHeight;//下方区域的高度
    private double bottomTitleHeight;
    private double bottomFlowHeight;
    private double bottomLockHeight;


    public ComponentHelper(int width, int height) {
        this.width = width;
        this.height = height;


        titleHeight =  height * titleHeightPer;
        contentHeight = (height * contentHeightPer);
        bottomHeight = height - titleHeight - contentHeight;

        gridWidth = (width - gridPadding * 2) / 3;
        waitGridHeight = (contentHeight - gridPadding) * waitGridHeightPer;
        mainContentGridHeight = (contentHeight - gridPadding) - waitGridHeight;

        bottomTitleHeight = bottomHeight * bottomTitlePer;
        bottomFlowHeight = bottomHeight * bottomFlowPer;
        bottomLockHeight = bottomHeight - bottomTitleHeight - bottomFlowHeight;


    }


    /**
     * 获取上部分的pane
     * @return
     */
    protected Pane getTitlePane(){
        double labelPadding = 10.0;
        AnchorPane pane = new AnchorPane();
        pane.setPrefWidth(width);
        pane.setPrefHeight(titleHeight);
        pane.setId("title");
        ObservableList<Node> children = pane.getChildren();
        Label stationName = new Label(Params.getProperty("stationName"));
        stationName.getStyleClass().add("titleLabel");
        children.add(stationName);
        Label roads = new Label(Params.getProperty("road") + "号油机/车道" + Params.getProperty("machine"));
        roads.getStyleClass().add("titleLabel");
        children.add(roads);
        AnchorPane.setLeftAnchor(stationName,labelPadding);
        AnchorPane.setRightAnchor(roads,labelPadding);
        return pane;
    }


    //============================中间content区域.开始============================================================================

    /**
     * 主要内容区域的
     * @return
     */
    protected Pane getContentPane(){
        StackPane pane = new StackPane();
        pane.setId("content");

        VBox vBox = new VBox();
        HBox h1 = GridCreator.createWaitArea(gridWidth, waitGridHeight,gridPadding);
        HBox h2 = GridCreator.createMainContentPane(gridWidth, mainContentGridHeight,gridPadding,piceWidth);
        VBox.setMargin(h2,new Insets(gridPadding,0,0,0));
        vBox.getChildren().add(h1);
        vBox.getChildren().add(h2);
        pane.getChildren().add(vBox);

        Pane picePanes = GridCreator.getPicePanes(width, contentHeight, gridWidth, waitGridHeight, gridPadding, piceWidth);


        pane.getChildren().add(picePanes);


        return pane;
    }
    static class GridCreator{
        /**
         * 创建完整的油品标签
         * @param width
         * @param contentHeight
         * @param gridWidth
         * @param gridHeight
         * @param gridPadding
         * @param piceWidth
         * @return
         */
        public static Pane getPicePanes(int width,double contentHeight,double gridWidth,double gridHeight, double gridPadding,int piceWidth){
            Pane picePanes = new Pane();
            picePanes.setPrefWidth(width);
            picePanes.setPrefHeight(contentHeight);

            double fw = (width - gridPadding * 2) / 3 / 2 - piceWidth;
            double fh = gridHeight - piceWidth;
            Pane p1 = GridCreator.getPicePane(fw,fh,"92",piceWidth);
            picePanes.getChildren().add(p1);

            fw = fw + gridWidth + gridPadding;
            Pane p2 = GridCreator.getPicePane(fw, fh, "95",piceWidth);
            picePanes.getChildren().add(p2);

            fw = fw + gridWidth + gridPadding;
            Pane p3 = GridCreator.getPicePane(fw, fh, "98",piceWidth);
            picePanes.getChildren().add(p3);

            return picePanes;
        }

        /**
         * 创建圆里面的油口标签
         * @param x
         * @param y
         * @param text
         * @return
         */
        public static Pane getPicePane(double x,double y,String text,double piceWidth){
            StackPane picePane = new StackPane();
            Arc arc = new Arc();
            arc.setFill(Color.rgb(213,213,213));
            arc.setCenterX(0);
            arc.setCenterY(0);
            arc.setRadiusX(piceWidth);
            arc.setRadiusY(piceWidth);
            arc.setStartAngle(0);
            arc.setLength(360);
            picePane.getChildren().add(arc);

            Label label = new Label(text);
            label.getStyleClass().add("oilTypeLabel");
            label.getStyleClass().add("oil_" + text);
            picePane.getChildren().add(label);

            picePane.setLayoutX(x);
            picePane.setLayoutY(y);

            return picePane;
        }


        /**
         * 创建一个主要内容区域的pane
         * @param gridWidth
         * @param gridHeight
         * @param gridPadding
         * @return
         */
        public static HBox createMainContentPane(double gridWidth,double gridHeight,double gridPadding,double topMargin){
            HBox hBox = new HBox();
            Insets insets = new Insets(0, gridPadding,0, 0);
            MacCarMainContentPane p1 = createMacCarMainContentPane(OIL_TYPE_92, topMargin, gridWidth, gridHeight);
            p1.updateContent(new MacCarMainContentPane.CarMainContentInfo("粤B123456", MacCarMainContentPane.CarMainContentInfo.STATUS_OILING,"会",20,400,20));
            HBox.setMargin(p1,insets);
            hBox.getChildren().add(p1);

            MacCarMainContentPane p2 = createMacCarMainContentPane(OIL_TYPE_95, topMargin, gridWidth, gridHeight);
            p2.updateContent(new MacCarMainContentPane.CarMainContentInfo("粤B123456", MacCarMainContentPane.CarMainContentInfo.STATUS_OILING,"免",20,400,20));
            HBox.setMargin(p2,insets);
            hBox.getChildren().add(p2);

            MacCarMainContentPane p3 = createMacCarMainContentPane(OIL_TYPE_98, topMargin, gridWidth, gridHeight);
            p3.updateContent(new MacCarMainContentPane.CarMainContentInfo("粤B123456", MacCarMainContentPane.CarMainContentInfo.STATUS_OILING,"专",20,400,20));
            hBox.getChildren().add(p3);

            return hBox;
        }

        /**
         * 创建一个等待的区域
         * @param gridWidth 宽度
         * @param gridHeight 高度
         * @return
         */
        public static HBox createWaitArea(double gridWidth,double gridHeight,double gridPadding){
            HBox h1 = new HBox();
            Insets insets = new Insets(0, gridPadding,0, 0);
            MacCarWaitContainer g1 = createMacCarWait(OIL_TYPE_92,gridWidth,gridHeight);
            g1.addWaitCar(new MacCar("粤LD107A",20,300, Arrays.asList(MacCar.TYPE_SPC)));
            g1.addWaitCar(new MacCar("粤LD107A",20,300,Arrays.asList(MacCar.TYPE_MIANMI)));
            HBox.setMargin(g1,insets);

            MacCarWaitContainer g2 = createMacCarWait(OIL_TYPE_95,gridWidth,gridHeight);
            g2.addWaitCar(new MacCar("粤LD107A",20,300, Arrays.asList(MacCar.TYPE_MEMBER)));
            g2.addWaitCar(new MacCar("粤LD107A",20,300,Arrays.asList(MacCar.TYPE_MIANMI)));
            HBox.setMargin(g2,insets);

            MacCarWaitContainer g3 = createMacCarWait(OIL_TYPE_98,gridWidth, gridHeight);
            g3.addWaitCar(new MacCar("粤LD107A",20,300, Arrays.asList(MacCar.TYPE_SPC)));
            g3.addWaitCar(new MacCar("粤LD107A",20,300,Arrays.asList(MacCar.TYPE_MIANMI)));

            h1.getChildren().add(g1);
            h1.getChildren().add(g2);
            h1.getChildren().add(g3);
            return h1;
        }

        public static MacCarWaitContainer createMacCarWait(String oilType, double gridWidth, double gridHeight){
            MacCarWaitContainer carWait = new MacCarWaitContainer(oilType);
            carWait.getStyleClass().add("grid");
            carWait.setPrefWidth(gridWidth);
            carWait.setPrefHeight(gridHeight);
            return carWait;
        }

        public static MacCarMainContentPane createMacCarMainContentPane(String oilType,double topMargin,double width,double height){
            MacCarMainContentPane pane = new MacCarMainContentPane(oilType,topMargin);
            pane.getStyleClass().add("mainContent-pane");
            pane.setPrefWidth(width);
            pane.setPrefHeight(height);
            return pane;
        }

/*        public static VBox getGrid(double gridWidth,double gridHeight){
            //中心内容区域车辆框间隔
            int pading = 3;
            VBox g3 = new VBox();
            g3.getStyleClass().add("grid");
            g3.setPrefWidth(gridWidth);
            g3.setPrefHeight(gridHeight);

            return g3;
        }*/

    }
    //============================中间content区域.结束============================================================================




    //============================下部区域.开始============================================================================
    public Pane getBottomPane(){
        VBox pane = new VBox();
        pane.setId("bottomPane");
        pane.setPrefWidth(width);
        pane.setPrefHeight(bottomHeight);

        Pane titlePane = BottomCreator.createTitlePane(width, bottomTitleHeight);
        pane.getChildren().add(titlePane);

        CarFlowPane carFlowPane = BottomCreator.createCarFlowPane(width, bottomFlowHeight);
        carFlowPane.setId("carFlowContainer");
        carFlowPane.addCar("粤B12345","会");
        carFlowPane.addCar("粤B12345","会");
        carFlowPane.addCar("粤B12345","会");
        carFlowPane.addCar("粤B12345","会");
        carFlowPane.addCar("粤B12345","会");
        carFlowPane.addCar("粤B12345","会");
        carFlowPane.addCar("粤B12345","会");
        carFlowPane.addCar("粤B12345","会");
        carFlowPane.addCar("粤B12345","会");
        carFlowPane.addCar("粤B12345","会");
        pane.getChildren().add(carFlowPane);


        Pane lockPane = BottomCreator.createLockPane(width, bottomLockHeight);
        pane.getChildren().add(lockPane);

        return pane;
    }

    static class BottomCreator{

        public static Pane createLockPane(double width,double height){
            VBox vBox = new VBox();
            vBox.setPrefHeight(height);
            vBox.setPrefWidth(width);
            vBox.setId("lockPane");
            AnchorPane pane = new AnchorPane();
            pane.getStyleClass().add("lock_anchor");

            vBox.setAlignment(Pos.CENTER);

            Label name = new Label("陆小畅");
            name.getStyleClass().add("staffName");
            AnchorPane.setLeftAnchor(name,10.0);
            pane.getChildren().add(name);

            Label lockLabel = new Label("锁屏");
            lockLabel.getStyleClass().add("lockLabel");
            AnchorPane.setRightAnchor(lockLabel,10.0);
            pane.getChildren().add(lockLabel);

            vBox.getChildren().add(pane);



            return vBox;
        }

        public static Pane createTitlePane(double width,double height){
            VBox vBox = new VBox();
            vBox.setPrefWidth(width);
            vBox.setPrefHeight(height);
            vBox.setId("bottom-title");
            vBox.setAlignment(Pos.CENTER);
            AnchorPane pane = new AnchorPane();
            vBox.getChildren().add(pane);

            Label l1 = new Label("进站车辆");
            l1.getStyleClass().add("bottom-title-text");
            pane.getChildren().add(l1);
            AnchorPane.setLeftAnchor(l1,10.0);

            Label l2 = new Label("更多");
            l2.getStyleClass().add("bottom-title-text");
            AnchorPane.setRightAnchor(l2,10.0);
            pane.getChildren().add(l2);

            return vBox;
        }

        public static CarFlowPane createCarFlowPane(double width,double height){
            CarFlowPane pane = new CarFlowPane();
            pane.setPrefWidth(width);
            pane.setPrefHeight(height);


            return pane;
        }


    }
    //============================下部区域.结束============================================================================

    static class Params{
        static String getProperty(String key){
            String value = System.getProperty(key);
            if(value == null){
                value = System.getenv(key);
            }
            return value;
        }
    }
}
